/*
编写 : Hexh
时间 : 2012-5-17 11:04:43
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;
using HMIS.Infrastructure.Facade;
using HMIS.Infrastructure.Core.Entities;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsCarbaseinfo
	/// </summary>
	public class FsCarbaseinfoController : BaseController
	{

		private readonly IFsCarbaseinfoFacade FsCarbaseinfoFacade;
		private readonly IHospUserFacade HospUserFacade;
		private readonly IInfraIdcardFacade InfraIdcardFacade;
		private readonly CurrentUser CurrentUser = new CurrentUser();

		public FsCarbaseinfoController(IFsCarbaseinfoFacade FsCarbaseinfoFacade, IHospUserFacade HospUserFacade, IInfraIdcardFacade InfraIdcardFacade)
		{
			this.FsCarbaseinfoFacade = FsCarbaseinfoFacade;
			this.HospUserFacade = HospUserFacade;
			this.InfraIdcardFacade = InfraIdcardFacade;
		}

		[Description("访问商品车库存管理页面")]
		[ViewPage]
		public ActionResult FsCarbaseinfo()
		{
			return View();
		}

		[Description("访问商品车出入库管理页面")]
		[ViewPage]
		public ActionResult FsCarBaseinfo_View_Store()
		{
			return View();
		}

		[Description("访问商品车录入页面")]
		[ViewPage]
		public ActionResult FsCarBaseinfo_View_Add()
		{
			return View();
		}

		[Description("访问商品车复核入库页面")]
		[ViewPage]
		public ActionResult FsCarBaseinfo_View_StoreAdd()
		{
			return View();
		}

		[Description("访问商品车库存查询页面")]
		[ViewPage]
		public ActionResult FsCarBaseinfo_View_StoreQuery()
		{
			return View();
		}

		[Description("访问商品车二维码打印页面")]
		[ViewPage]
		public ActionResult FsCarBaseinfo_View_PrintQrcode()
		{
			return View();
		}

		[ExtResult]
		[Description("录入修改整车库存信息")]
		public ActionResult SaveOrUpdate()
		{
			FsCarbaseinfo entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				if (String.IsNullOrEmpty(CurrentUser.UserInfo.DepartmentId))
					throw new ValidationException("当前用户[" + CurrentUser.UserInfo.Name + "]无法录入整车信息。<br>请检查用户是否配置所属展场信息。");

				entity = new FsCarbaseinfo
				{
					ICCard = new InfraIdcard
					{
						CardUid = Request["ICCardUId"],
						DeptId = CurrentUser.UserInfo.DepartmentId,
						Type = InfraIdcardType.CarInfoCard,
					},
					KeyICCard = new InfraIdcard
					{
						CardUid = Request["KeyICCardUId"],
						DeptId = CurrentUser.UserInfo.DepartmentId,
						Type = InfraIdcardType.CarKeyCard,
					},
					ICCardUId = Request["ICCardUId"],
					KeyICCardUId = Request["KeyICCardUId"],
					DepartmentId = CurrentUser.UserInfo.DepartmentId,
					Brand = Request["Brand"],
					Series = Request["Series"],
					Model = Request["Model"],
					Color = Request["Color"],
					EnginId = Request["EnginId"],
					FrameId = Request["FrameId"],
					ProdDate = Request["ProdDate"] == null ? DateTime.MinValue : DateTime.Parse(Request["ProdDate"]),
					Mileage = Request["Mileage"] == null ? 0 : decimal.Parse(Request["Mileage"]),
					MarketPrice = Request["MarketPrice"] == null ? 0 : decimal.Parse(Request["MarketPrice"]),
					AtmGjb = Request["AtmGjb"] == null ? 0 : int.Parse(Request["AtmGjb"]),
					AtmBt = Request["AtmBt"] == null ? 0 : int.Parse(Request["AtmBt"]),
					AtmSms = Request["AtmSms"] == null ? 0 : int.Parse(Request["AtmSms"]),
					AtmDyq = Request["AtmDyq"] == null ? 0 : int.Parse(Request["AtmDyq"]),
					AtmBxk = Request["AtmBxk"] == null ? 0 : int.Parse(Request["AtmBxk"]),
					AtmKey = Request["AtmKey"] == null ? 0 : int.Parse(Request["AtmKey"]),
					AtmMast = Request["AtmMast"] == null ? 0 : int.Parse(Request["AtmMast"]),
					AtmWiper = Request["AtmWiper"] == null ? 0 : int.Parse(Request["AtmWiper"]),
					AtmCertificate = Request["AtmCertificate"] == null ? 0 : int.Parse(Request["AtmCertificate"]),
					CustId = Request["CustId"],
					State = FsCarState.ToStore,
					Description = Request["Description"],
				};
			}
			else
			{
				// get the entity.
				entity = FsCarbaseinfoFacade.Get(Request["Id"]);
				// 是否可以修改.
				/*
				if (entity.State != FsCarState.ToStore)
				{
					throw new ValidationException("整车信息已入库，不可修改!");
				}
				 */
				// set new value..
				entity.ICCard.CardUid = Request["ICCardUId"];
				entity.KeyICCard.CardUid = Request["KeyICCardUId"];
				entity.ICCardUId = Request["ICCardUId"];
				entity.KeyICCardUId = Request["KeyICCardUId"];
				entity.Brand = Request["Brand"];
				entity.Series = Request["Series"];
				entity.Model = Request["Model"];
				entity.Color = Request["Color"];
				entity.EnginId = Request["EnginId"];
				entity.FrameId = Request["FrameId"];
				entity.ProdDate = Request["ProdDate"] == null ? DateTime.MinValue : DateTime.Parse(Request["ProdDate"]);
				entity.Mileage = Request["Mileage"] == null ? 0 : decimal.Parse(Request["Mileage"]);
				entity.MarketPrice = Request["MarketPrice"] == null ? 0 : decimal.Parse(Request["MarketPrice"]);
				entity.AtmGjb = Request["AtmGjb"] == null ? 0 : int.Parse(Request["AtmGjb"]);
				entity.AtmBt = Request["AtmBt"] == null ? 0 : int.Parse(Request["AtmBt"]);
				entity.AtmSms = Request["AtmSms"] == null ? 0 : int.Parse(Request["AtmSms"]);
				entity.AtmDyq = Request["AtmDyq"] == null ? 0 : int.Parse(Request["AtmDyq"]);
				entity.AtmBxk = Request["AtmBxk"] == null ? 0 : int.Parse(Request["AtmBxk"]);
				entity.AtmKey = Request["AtmKey"] == null ? 0 : int.Parse(Request["AtmKey"]);
				entity.AtmMast = Request["AtmMast"] == null ? 0 : int.Parse(Request["AtmMast"]);
				entity.AtmWiper = Request["AtmWiper"] == null ? 0 : int.Parse(Request["AtmWiper"]);
				entity.AtmCertificate = Request["AtmCertificate"] == null ? 0 : int.Parse(Request["AtmCertificate"]);
				entity.CustId = Request["CustId"];
				entity.State = FsCarState.ToStore;
				entity.Description = Request["Description"];
			}
			// 登记车辆信息
			FsCarbaseinfoFacade.SaveOrUpdate(entity);
			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}

		[ExtResult]
		[Description("整车信息复核入库")]
		public ActionResult CarStorageIn()
		{
			if (!String.IsNullOrEmpty(Request["Ids"]))
			{
				string[] Ids = Request["Ids"].Split(',');
				for (int i = 0; i < Ids.Length; i++)
				{
					if (!string.IsNullOrEmpty(Ids[i]))
					{
						FsCarbaseinfo entity;
						// get the entity.
						entity = FsCarbaseinfoFacade.Get(Ids[i]);
						// 是否可以修改.
						if (entity.State != FsCarState.ToStore)
						{
							throw new Exception("整车信息状态为[ " + entity.State.ToString() + " ],不可进行入库操作.");
						}
						entity.StoreDate = DateTime.Now;
						entity.State = FsCarState.InStore;
						// 保存信息
						FsCarbaseinfoFacade.SaveOrUpdate(entity);
					}
				}
				return this.JsonFormat(new ExtResult { success = true });
			}
			return this.JsonFormat(new ExtResult { success = false, msg = "未找到整车信息!" });
		}

		[ExtResult]
		[Description("删除FsCarbaseinfo基本资料")]
		public ActionResult Delete()
		{
			FsCarbaseinfoFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[ExtResult]
		[Description("物理删除FsCarbaseinfo基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsCarbaseinfoFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsCarbaseinfo清单")]
		public ActionResult GetPlist()
		{
			long total;
			CurrentUser CUser = new CurrentUser();
			var data = FsCarbaseinfoFacade.GetPlist(
				Request["q_query"],
				Request["state"],
				CUser.UserInfo.DepartmentId,
				CUser.UserInfo.UserGroup,
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			logger.Info("-------------------------------------");
			return this.JsonFormat(
				new ExtGirdData<FsCarbaseinfo> { total = total, data = data },
				new string[] { "Department" });
		}

		[Description("获取FsCarbaseinfo信息")]
		public ActionResult GetById()
		{
			var entity = FsCarbaseinfoFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsCarbaseinfo> { data = new FsCarbaseinfo[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[Description("生成商品车二维码")]
		public void GetCarQrCodeById()
		{
			var entity = FsCarbaseinfoFacade.Get(Request["Id"]);
			//生成信息
			string infoStr = string.Format(@"[ID:{0}]
[品牌:{1}]
[系列:{2}]
[型号:{3}]
[颜色:{4}]
[车架号:{5}]
[发动机号:{6}]
[出厂日期:{7}]",
		   entity.Id, entity.Brand, entity.Series,
		   entity.Model, entity.Color, entity.FrameId,
		   entity.EnginId, entity.ProdDate.ToString("yyyy年MM月dd日"));
			//生成二维码并返回client
			base.GetQrCode(infoStr, Request["size"]);
		}

		[ExtResult]
		[Description("判断FsCarbaseinfo字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsCarbaseinfoFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
